package com.uzhie.jt808.jt1078.entity;

import com.uzhie.jt808.JT808MsgBody;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.jt808.msg.entity.MyBuffer;
import com.uzhie.jt808.util.DateUtil;

/**
 * 终端上传音视频资源列表类型
 * Created by lilongfei on 2018/7/29.
 */
public class JT_1205_Catalog implements JT808MsgBody {

    //通道
    byte channel;
    //查询起始时间
    String beginTime;
    //查询终止时间
    String endTime;
    //报警标识
    byte[] warns;
    //音视频资源类型 0 音视频 1 音频 2 视频
    byte type;
    //主码流,子码流 1 主码流 2 子码流
    byte stream;
    //远程存储位置 1 主存储器 2 灾备存储器
    byte local;
    //文件大小
    int size;
    //时长
    long duration;

    public void setChannel(int channel){
        byte[] channels = BitConverter.GetBytes(channel);
        setChannel(channels[3]);
    }

    public void setWarns(long warns){
        setWarns(BitConverter.getByte64(warns));
    }

    public void setType(int type){
        byte[] types = BitConverter.GetBytes(type);
        setType(types[3]);
    }

    public void setStream(int stream){
        byte[] streams = BitConverter.GetBytes(stream);
        setStream(streams[3]);
    }

    public void setLocal(int local){
        byte[] locals = BitConverter.GetBytes(local);
        setLocal(locals[3]);
    }

    public byte getChannel() {
        return channel;
    }

    public int getIntChannel() {
        return BitConverter.ToUInt32(channel);
    }

    public void setChannel(byte channel) {
        this.channel = channel;
    }

    public String getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(String beginTime) {
        this.beginTime = beginTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public byte[] getWarns() {
        return warns;
    }

    public int getIntWarns() {
        return BitConverter.ToUInt16(warns, 0);
    }

    public void setWarns(byte[] warns) {
        this.warns = warns;
    }

    public byte getType() {
        return type;
    }

    public int getIntType() {
        return BitConverter.ToUInt32(type);
    }

    public void setType(byte type) {
        this.type = type;
    }

    public byte getStream() {
        return stream;
    }

    public int getIntStream() {
        return BitConverter.ToUInt32(stream);
    }

    public void setStream(byte stream) {
        this.stream = stream;
    }

    public byte getLocal() {
        return local;
    }

    public int getIntLocal() {
        return BitConverter.ToUInt32(local);
    }

    public void setLocal(byte local) {
        this.local = local;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    @Override
    public byte[] WriteToBytes() {
        MyBuffer buff = new MyBuffer();
        buff.put(getChannel());
        //设置起始时间
        buff.put(Byte.parseByte(getBeginTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getBeginTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getBeginTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getBeginTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getBeginTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getBeginTime().substring(2, 4), 16));
        //设置终止时间
        buff.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        //设置警告标识
        buff.put(getWarns());
        //音视频资源类型
        buff.put(getType());
        //码流类型
        buff.put(getStream());
        //远程存储位置
        buff.put(getLocal());
        //设置文件大小
        buff.put(getSize());
        return buff.array();
    }

    @Override
    public void ReadFromBytes(byte[] body) {
        MyBuffer buffer = new MyBuffer(body);
        setChannel(buffer.get());
        byte[] start = buffer.gets(6);
        setBeginTime("20" + String.format("%02X", start[0]) + "-"
                + String.format("%02X", start[1]) + "-"
                + String.format("%02X", start[2]) + " "
                + String.format("%02X", start[3]) + ":"
                + String.format("%02X", start[4]) + ":"
                + String.format("%02X", start[5]));
        byte[] end = buffer.gets(6);
        setEndTime("20" + String.format("%02X", end[0]) + "-"
                + String.format("%02X", end[1]) + "-"
                + String.format("%02X", end[2]) + " "
                + String.format("%02X", end[3]) + ":"
                + String.format("%02X", end[4]) + ":"
                + String.format("%02X", end[5]));
        setWarns(buffer.gets(6));
        setType(buffer.get());
        setStream(buffer.get());
        setLocal(buffer.get());
        setSize(BitConverter.ToUInt16(buffer.gets(4), 0));
        setDuration((DateUtil.getDate(getEndTime()).getTime() - DateUtil.getDate(getBeginTime()).getTime())/1000);
    }

    public String toString(){
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append(String.format(
                "通道号：%1$s,起始时间：%2$s,结束事件：%3$s,报警标识：%4$s,文件类型：%5$s,码流：%6$s,存储位置：%7$s,文件大小：%7$s",
                getChannel(), getBeginTime(), getEndTime(), getWarns(),
                getType(), getStream(), getLocal(),getSize()));
        return sBuilder.toString();
    }
}
